// components/floatingPlayer/index.js
import audioManager from '../../utils/audioManager.js'

Component({
  properties: {},
  
  data: {
    // 播放状态
    playState: {
      isPlaying: false,
      isPaused: false,
      isLoading: false,
      progress: 0
    },
    
    // 当前音频信息
    currentAudio: {
      title: '未知音频',
      category: ''
    },
    
    // 显示状态
    visible: false,
    
    // 动画状态
    animationData: {}
  },
  
  lifetimes: {
    attached() {
      // 组件初始化时绑定事件监听
      this.bindAudioManagerEvents()
      
      // 获取当前状态
      this.updateFromManager()
    },
    
    detached() {
      // 组件销毁时解绑事件监听
      this.unbindAudioManagerEvents()
    }
  },
  
  methods: {
    /**
     * 绑定音频管理器事件
     */
    bindAudioManagerEvents() {
      // 使用bind保持this指向
      this.boundStateChangeHandler = this.onStateChange.bind(this)
      this.boundAudioChangeHandler = this.onAudioChange.bind(this)
      
      audioManager.addEventListener('stateChange', this.boundStateChangeHandler)
      audioManager.addEventListener('audioChange', this.boundAudioChangeHandler)
    },
    
    /**
     * 解绑音频管理器事件
     */
    unbindAudioManagerEvents() {
      if (this.boundStateChangeHandler) {
        audioManager.removeEventListener('stateChange', this.boundStateChangeHandler)
      }
      if (this.boundAudioChangeHandler) {
        audioManager.removeEventListener('audioChange', this.boundAudioChangeHandler)
      }
    },
    
    /**
     * 状态变化回调
     */
    onStateChange: function(playState, showFloatingPlayer) {
      this.setData({
        playState: {
          isPlaying: playState.isPlaying,
          isPaused: playState.isPaused,
          isLoading: playState.isLoading,
          progress: playState.progress
        },
        visible: showFloatingPlayer
      })
      
      // 显示/隐藏动画
      if (showFloatingPlayer) {
        this.showAnimation()
      } else {
        this.hideAnimation()
      }
    },
    
    /**
     * 音频变化回调
     */
    onAudioChange: function(currentAudio) {
      this.setData({
        currentAudio: {
          title: currentAudio.title || '未知音频',
          category: currentAudio.category || ''
        }
      })
    },
    
    /**
     * 从管理器更新状态
     */
    updateFromManager() {
      const state = audioManager.getState()
      this.onStateChange(state.playState, state.showFloatingPlayer)
      this.onAudioChange(state.currentAudio)
    },
    
    /**
     * 显示动画
     */
    showAnimation() {
      const animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'ease-out'
      })
      
      animation.translateX(0).opacity(1).step()
      
      this.setData({
        animationData: animation.export()
      })
    },
    
    /**
     * 隐藏动画
     */
    hideAnimation() {
      const animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'ease-in'
      })
      
      animation.translateX(200).opacity(0).step()
      
      this.setData({
        animationData: animation.export()
      })
    },
    
    /**
     * 播放/暂停切换
     */
    togglePlay() {
      const success = audioManager.togglePlay()
      if (!success) {
        wx.showToast({
          title: '播放失败',
          icon: 'none'
        })
      }
    },
    
    /**
     * 上一首
     */
    playPrevious() {
      audioManager.playPrevious()
    },
    
    /**
     * 下一首
     */
    playNext() {
      audioManager.playNext()
    },
    
    /**
     * 点击浮窗 - 跳转到播放页面
     */
    onFloatingPlayerTap() {
      const state = audioManager.getState()
      if (state.currentAudio.id) {
        const playlist = encodeURIComponent(JSON.stringify(state.playlist))
        wx.navigateTo({
          url: `/pages/audioPlayer/index?audioId=${state.currentAudio.id}&playlist=${playlist}&index=${state.currentIndex}`
        })
      }
    },
    
    /**
     * 关闭浮窗
     */
    closeFloatingPlayer() {
      audioManager.close()
    },
    
    /**
     * 阻止事件冒泡
     */
    preventBubble() {
      // 阻止点击事件冒泡到父元素
    }
  }
})
